package com.example.ln.zyfypt_119ln.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Switch;
import android.widget.Toast;

import com.example.ln.zyfypt_119ln.R;


public class LoginActivity extends AppCompatActivity {
    private Button button;
    private EditText etname,etpass;
    private Switch sw;

    private String username="",password="";//用户名密码
    private SharedPreferences sp;

    private void saveSP() {
        SharedPreferences.Editor editor=sp.edit();
        editor.putString("username",username);
        editor.putString("password",password);
        editor.putBoolean("remember",sw.isChecked());
        editor.commit();//提交保存
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        init();
        //实例化sp
        sp=getSharedPreferences("login",MODE_PRIVATE);
        readSP();
        Button btn=(Button)findViewById(R.id.button_denglu);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                username=etname.getText().toString();
                password=etpass.getText().toString();
                if(username.equals("ln")&& password.equals("119"))
                {
                    Intent intent=new Intent(LoginActivity.this, MainActivity.class);
                    startActivity(intent);
                    Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
                    saveSP();
                }

            }
        });
        btn = (Button) findViewById(R.id.button_zhuce);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent=new Intent(LoginActivity.this,RegisterActivity.class);
                startActivity(intent);

            }
        });

    }

    private void readSP() {
        String name=sp.getString("username",null);
        String pass=sp.getString("password",null);
        Boolean b=sp.getBoolean("remember",false);
        if(b)
        {
            etname.setText(name);
            etpass.setText(pass);
            sw.setChecked(b);
        }
    }

    private void init() {
        button=(Button)findViewById(R.id.button_denglu);
        etname=(EditText)findViewById(R.id.editText);
        etpass=(EditText)findViewById(R.id.editText2);
        sw=(Switch) findViewById(R.id.switch1);

    }
}